

const conv = require('./w3g-conv')
const fs = require('fs');
const bin = require('./binary');

// to know when there is a need to load next block
const MAX_DATABLOCK = 1500;
// for preventing duplicated actions
const ACTION_DELAY = 1000;
// to know how long it may take after buying a Tome of Retraining to retrain a hero
const RETRAINING_TIME = 15000;



module.exports.replay = function (filename, parse_actions, parse_chat) {
    var fp, data, leave_unknown, continue_game, referees, time, pause, leaves, errors, header, game,  players, teams, chat, filename ;
    var max_datablock = MAX_DATABLOCK;

    parse_actions = parse_actions || true;
    parse_actions = parse_chat || true;
    game = {}
    game['player_count'] = 0;

    fs.readFile(filename, function (err, buffer_data) {
        if (err) {
            console.log(err);
        } else {
           parseheader(buffer_data) 
           parsedata(buffer_data)
        }
    });
    
   

    var parseheader = function (buffer_data){
        let data = buffer_data.slice(0,48)
        let header1 = bin.unpack('a28intro/Vheader_size/Vc_size/Vheader_v/Vu_size/Vblocks', data.toString('latin1'));

        if('Warcraft III recorded game\u001a' !== header1['intro']){
            throw new Error( "Not a replay file" ) 
        }
        if (header1['header_v'] == 0) {
            let data2 = buffer_data.slice(48, 48+16);
            let data2_1 = bin.unpack('a4ident/Vmajor_v/vbuild_v/vflags/Vlength/Vchecksum', data2.toString('latin1'));
            header1['ident']      = 'WAR3';
            header1['major_v']  = data2_1['major_v'];
            header1['build_v']  = data2_1['build_v'];
            header1['flags']    = data2_1['flags'];
            header1['length']   = data2_1['length'];
            header1['checksum'] = data2_1['checksum'];

        } else {
            let data2 = buffer_data.slice(48, 48+20);
            let data2_1 = bin.unpack('a4ident/Vmajor_v/vbuild_v/vflags/Vlength/Vchecksum', data2.toString('latin1'));
            header1['ident']    = data2_1['ident'];
            header1['major_v']  = data2_1['major_v'];
            header1['build_v']  = data2_1['build_v'];
            header1['flags']    = data2_1['flags'];
            header1['length']   = data2_1['length'];
            header1['checksum'] = data2_1['checksum'];
        }
        this.header = header1
    }
    var parsedata = function (buffer_data){
        blocks_count = this.header['blocks'];
        let poststion = this.header['header_size'];

        for (let i=0; i<blocks_count; i++) {
            
            // 3.0 [Data block header]
            let block_header = bin.unpack('vc_size/vu_size/Vchecksum', buffer_data.slice(poststion, poststion + 8 ).toString('latin1'));
            poststion += 8
            
            let temp =  buffer_data.slice(poststion , poststion + block_header['c_size'] );
            poststion += block_header['c_size'] 
             
            let temp2 = temp.slice(2, temp.length-4)
            temp2[0] = (temp2[0] | 1 ) -1
            /*
            php gzinflate 
            if (temp2 = gzinflate($temp)) {  
				$this->data .= $temp;
			} else {
				exit($this->filename.': Incomplete replay file');
			}*/
        }
    }

    /*

    function parsedata() {
		fseek($this->fp, $this->header['header_size']);
		$blocks_count = $this->header['blocks'];
		for ($i=0; $i<$blocks_count; $i++) {
			// 3.0 [Data block header]
			$block_header = @unpack('vc_size/vu_size/Vchecksum', fread($this->fp, 8));
			$temp = fread($this->fp, $block_header['c_size']);
			$temp = substr($temp, 2, -4);
			// the first bit must be always set, but already set in replays with modified chatlog (why?)
			$temp{0} = chr(ord($temp{0}) | 1);
			if ($temp = gzinflate($temp)) {
				$this->data .= $temp;
			} else {
				exit($this->filename.': Incomplete replay file');
			}
	
			// 4.0 [Decompressed data]
			if ($i == 0) {
				$this->data = substr($this->data, 4);
				$this->loadplayer();
				$this->loadgame();
			} elseif ($blocks_count - $i < 2) {
				$this->max_datablock = 0;
			}
	
			if ($this->parse_chat || $this->parse_actions) {
				$this->parseblocks();
			} else {
				break;
			}
		}
    }
    
    */




}
